package com.gpjcjl.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.lifecycle.ViewModelProvider;

import com.base.lib.ToastUtil;
import com.gpjcjl.BaseActivity;
import com.gpjcjl.R;
import com.gpjcjl.entity.Record;
import com.gpjcjl.util.DataProvider;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;

import java.util.Locale;

import butterknife.BindView;
import butterknife.OnClick;

public class RecordUpdateActivity extends BaseActivity {

    @BindView(R.id.title)
    TextView tvTitle;
    @BindView(R.id.tv_date)
    TextView tvDate;
    @BindView(R.id.tv_property)
    TextView tvProperty;
    @BindView(R.id.edit_ambient)
    EditText editAmbient;
    @BindView(R.id.edit_water)
    EditText editWater;
    @BindView(R.id.tv_specs)
    TextView tvSpecs;
    @BindView(R.id.edit_start_num)
    EditText editStartNum;
    @BindView(R.id.edit_end_num)
    EditText editEndNum;
    @BindView(R.id.edit_report_num)
    EditText editReportNum;

    private RecordUpdateViewModel viewModel;

    private Record record;
    private boolean isRemote;

    @Override
    protected int getContentView() {
        return R.layout.activity_update;
    }

    public static void startIntent(Context context, Record record, boolean isRemote) {
        Intent it = new Intent(context, RecordUpdateActivity.class);
        it.putExtra("record", record);
        it.putExtra("isRemote", isRemote);
        context.startActivity(it);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewModel = new ViewModelProvider(this).get(RecordUpdateViewModel.class);
        record = (Record) getIntent().getExtras().get("record");
        isRemote = getIntent().getExtras().getBoolean("isRemote", false);
        tvProperty.setText(record.getProperty());
        tvDate.setText(record.getCheck_time());
        editWater.setText(record.getWater());
        editAmbient.setText(record.getAmbient());
        tvSpecs.setText(record.getSpecs());
        editStartNum.setText(record.getStart_num());
        editEndNum.setText(record.getEnd_num());
        editReportNum.setText(record.getReport_num());
    }

    @OnClick({R.id.tv_date, R.id.tv_property, R.id.btn_save, R.id.iv_add, R.id.tv_specs})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_date:
                showDatePicker();
                break;
            case R.id.tv_property:
            case R.id.iv_add:
                PropertyListActivity.startIntentForResult(this, 1);
                break;
            case R.id.tv_specs:
                showSpecsDialog();
                break;
            case R.id.btn_save:
                String property = tvProperty.getText().toString().trim();
                String date = tvDate.getText().toString().trim();
                String water = editWater.getText().toString().trim();
                String specs = tvSpecs.getText().toString().trim();
                String ambient = editAmbient.getText().toString().trim();
                String startNum = editStartNum.getText().toString().trim();
                String endNum = editEndNum.getText().toString().trim();
                String reportNum = editReportNum.getText().toString().trim();

                if (TextUtils.isEmpty(property)) {
                    ToastUtil.showToast(mContext, "请选择产权单位");
                } else if (TextUtils.isEmpty(date)) {
                    ToastUtil.showToast(mContext, "请选择检测日期");
                } else if (TextUtils.isEmpty(ambient)) {
                    ToastUtil.showToast(mContext, "请填写环境温度");
                } else if (TextUtils.isEmpty(water)) {
                    ToastUtil.showToast(mContext, "请填写水温");
                } else if (TextUtils.isEmpty(specs)) {
                    ToastUtil.showToast(mContext, "请选择钢瓶规格");
                } else if (TextUtils.isEmpty(startNum)) {
                    ToastUtil.showToast(mContext, "请选填写正确的起始编号");
                } else {
                    record.setProperty(property);
                    record.setCheck_time(date);
                    record.setWater(water);
                    record.setAmbient(ambient);
                    record.setSpecs(specs);
                    record.setStart_num(startNum);
                    record.setEnd_num(endNum);
                    record.setReport_num(reportNum);
                    showLoading();
                    viewModel.updateRecord(isRemote, record).observe(this, result -> {
                        dismissLoading();
                        if (result.isSuccess()) {
                            finish();
                        }
                        ToastUtil.showToast(mContext, result.getMsg());
                    });
                }
                break;
            default:
                break;
        }
    }

    private void showSpecsDialog() {
        String[] items = DataProvider.getBottleSpecsList().toArray(new String[0]);
        new QMUIDialog.MenuDialogBuilder(mContext).addItems(items, (dialog, which) -> {
            tvSpecs.setText(items[which]);
            dialog.dismiss();
        }).show();
    }

    private void showDatePicker() {
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        View view = getLayoutInflater().inflate(R.layout.dialog_datepicker, null);
        DatePicker datePicker = view.findViewById(R.id.date_picker);
        //设置日期简略显示 否则详细显示 包括:星期\周
        datePicker.setCalendarViewShown(false);
        //设置date布局
        builder.setView(view)
                .setTitle("选择日期")
                .setNegativeButton("取消", null)
                .setPositiveButton("确定", (dialog, which) -> {
                    int year = datePicker.getYear();
                    int month = datePicker.getMonth() + 1;
                    int dayOfMonth = datePicker.getDayOfMonth();
                    tvDate.setText(String.format(Locale.getDefault(), "%d-%d-%d", year, month, dayOfMonth));
                    dialog.cancel();
                })
                .create().show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }
        if (1 == requestCode && data != null && data.hasExtra("result")) {
            String result = data.getStringExtra("result");
            tvProperty.setText(result);
        }
    }
}
